package com.afkt.project.ui.slice;

import com.afkt.project.model.bean.GifSizeFilter;
import com.afkt.project.model.bean.MyListBean;
import com.afkt.project.ui.adapter.MyListAdapter;
import com.afkt.project.util.LogUtils;
import com.afkt.project.util.ToastUtil;
import com.ohos.afkt.ResourceTable;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MatisseAbility;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.SimpleImageEngine;
import com.zhihu.matisse.filter.Filter;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class SelectPictureSlice extends AbilitySlice {

    private ListContainer listContainer;
    private MyListAdapter mAdapter;
    private static final int REQUEST_CODE = 23;
    private static final int MY_PERMISSIONS_REQUEST_CAMERA = 23;
    List<Uri> mSelected;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_layout_selectPicture);
        initList();
    }

    private void initList() {
        List<MyListBean> list = initData();
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_ringtone_list);
        mAdapter = new MyListAdapter(list, this);
        listContainer.setItemProvider(mAdapter);
        listContainer.setItemClickedListener((listContainer, component, position, l) -> {
            MyListBean bean= (MyListBean)mAdapter.getItem(position);
            if(position==0){
                if("选择图片".equals(((MyListBean)mAdapter.getItem(position)).getName())){
                    Matisse.from(SelectPictureSlice.this)
                            .choose(MimeType.ofAll())
                            .countable(true)
                            .maxSelectable(9)
                            .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
                            .thumbnailScale(0.85f)
                            .imageEngine(new SimpleImageEngine())
                            .showPreview(false) // Default is `true`
                            .forResult(MY_PERMISSIONS_REQUEST_CAMERA);
                }else{
                    ToastUtil.toastShowLong(SelectPictureSlice.this,bean.getName(),ToastUtil.TOAST_SHORT_TIME);
                }
            }else{
                ToastUtil.toastShowLong(SelectPictureSlice.this,bean.getName(),ToastUtil.TOAST_SHORT_TIME);
            }

        });
    }

    private List<MyListBean> initData() {
        List<MyListBean> lists = new ArrayList<>();
        lists.add(new MyListBean("选择图片"));
        return lists;
    }
    private List<MyListBean> setData(ArrayList<Uri> uriArrayList) {
        List<MyListBean> lists = new ArrayList<>();
        uriArrayList.forEach(uri -> lists.add(new MyListBean(uri.toString())));
        return lists;
    }


    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent data) {
        super.onAbilityResult(requestCode, resultCode, data);
        if (resultCode != MatisseAbility.RESULT_OK){
            return;
        }
        if (requestCode == MY_PERMISSIONS_REQUEST_CAMERA) {
            ArrayList<Uri> uriArrayList = data.getSequenceableArrayListParam(MatisseAbility.EXTRA_RESULT_SELECTION);
            LogUtils.debug("Matisse", "mSelected: " + mSelected);
            mAdapter = new MyListAdapter(setData(uriArrayList), this);
            listContainer.setItemProvider(mAdapter);
        }
    }

}
